import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import mall from "@src/store/mall";
import base from "@src/store/base";
import router from "@src/router";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    fields: ['chooseAddress']
  }, {
    store: base,
    fields: ['isiPhoneX', 'themeColor', 'themeTitleColor', 'barHeight']
  }],
  properties: {
    delivery: {
      type: Object,
      value: null
    },
    defaultAddress: {
      type: Object,
      value: null,
      observer(newVal, oldVal, changedPath) {
      }
    },
    groupInfo:{
      type: Object,
      value: null,
      observer(newVal, oldVal, changedPath) {
      }
    },
    deliveryType:{
      type:String,
      value:"",
    },
  },
  data: {
    address: null,
    leaderCollect:false
  },
  observers: {
    'chooseAddress,defaultAddress': function (chooseAddress, defaultAddress) {
      if (chooseAddress) {
        this.setAddress(chooseAddress)
        return
      }
      if (defaultAddress) {
        this.setAddress(defaultAddress)
      }
      if(!chooseAddress&&!defaultAddress){
        this.setAddress();
      }
    }
  },
  methods: {
    handleReservedPhone(e){
      this.triggerEvent('handleReservedPhone',e.detail)
    },
    handleLeaderCollect(e){
      this.setData({leaderCollect:e.detail})
      this.triggerEvent("handleLeaderCollect",e.detail)
    },
    handleToChoose() {
      router.push({
        name: "mall/address"
      })
    },
    setAddress(address=null) {
      // 发送更新地址信息
      this.triggerEvent('address', address);
      this.setData({address: address})
    }
  }
});
